﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HumanWorkflow.SemanticModel;

namespace OrderWorkflow
{
	public partial class OrderWorkflowWithFramework
	{
        public OrderWorkflowWithFramework()
            : base(CustomInitializeSemanticModel())
        {
        }

        static HumanWorkflowSemanticModel CustomInitializeSemanticModel()
        {
            HumanWorkflowSemanticModel semanticModel = OrderWorkflowWithFramework.InitializeSemanticModel();

            FinalStep finalStep = (FinalStep)((IList<Step>)semanticModel.Steps).Where<Step>(s => s is FinalStep).First<Step>();

            foreach (Step step in semanticModel.Steps)
            {
                if (! (step is FinalStep))
                {
                    semanticModel.AddTransition(step.Name, finalStep.Name, step.Name + "StopWorkflow");
                }
            
            }

            return semanticModel;
        }

        public bool StocksAreOk { get; set; }

        public override string ExecuteAutomaticStep(Step executedStep)
        {
            string nextTransitionName;

            switch (executedStep.Name)
            {
                case OrderWorkflowWithFrameworkSteps.AreAllItemsOnStockDecisionStep:
                    if (StocksAreOk)
                    {
                        nextTransitionName = OrderWorkflowWithFrameworkTransitions.ItemsAreAvailable;
                    }
                    else
                    {
                        nextTransitionName = OrderWorkflowWithFrameworkTransitions.ItemsAreMissing;                 
                    }

                    break;
                case OrderWorkflowWithFrameworkSteps.SendNotificationEmailToCustomerAutomaticStep:
                    
                    //TODO: send an email here to the customer
                    
                    nextTransitionName = OrderWorkflowWithFrameworkTransitions.EmailSent;
                    break;

                default:
                    throw new NotImplementedException(executedStep.Name + " is not implemented in ExecuteAutomaticStep override.");

            }

            return nextTransitionName;
        }

	}
}
